package com.jsoup.jsoup;

import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/*** Description:
* @author: whz
* @date: 2022/2/19 17:48
* @param:
* @return:*/
@RestController
public class CityController {
    @Autowired
    private AreaService areaService;

    @SneakyThrows
    @GetMapping("test")
    private void city() {
        //国家统计局地址
        String url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/index.html";

        //链接到目标地址
        Connection connect = Jsoup.connect(url);
        //设置useragent,设置超时时间，并以get请求方式请求服务器
        Document document = null;
        {
            try {
                document = connect.userAgent("Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)").
                        timeout(6000).ignoreContentType(true).get();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //获取省份列表
        ListIterator<Element> elements = document.getElementsByClass("provincetr").listIterator();
        List areaList = new ArrayList<>();
        int i = 0;
        while (elements.hasNext()) {
            Element tr = elements.next();
            ListIterator<Element> tds = tr.getElementsByTag("td").listIterator();
            while (tds.hasNext()) {
                Area province = new Area();
                i++;
                Element td = tds.next();
                province.setName(td.text());
                province.setId(i);
                province.setParent_id(0);
                areaList.add(province);
                Elements a1 = td.getElementsByTag("a");
                url = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/" + a1.attr("href");
                connect = Jsoup.connect(url);
                document = connect.
                        timeout(6000).ignoreContentType(true).get();
//                获取省份下得城市
                ListIterator<Element> countys = document.getElementsByClass("citytr").listIterator();

                while (countys.hasNext()) {
                    ListIterator<Element> couna = countys.next().getElementsByTag("td").listIterator();
                    Area county = new Area();
                    while (couna.hasNext()) {
                        Element a = couna.next();
                        i++;
                        county.setParent_id(province.getId());
                        county.setId(i);
                        county.setName(a.text().trim());
                    }
                    areaList.add(county);
                }
            }
        }
        areaService.saveBatch(areaList);
    }
}
